package cn.tedu.bs.admin.controller;

import cn.tedu.bs.admin.pojo.param.BankAddNewParam;
import cn.tedu.bs.admin.pojo.param.BankUpdateInfoParam;
import cn.tedu.bs.admin.pojo.vo.BankListItemVO;
import cn.tedu.bs.admin.pojo.vo.BankStandardVO;
import cn.tedu.bs.admin.pojo.vo.PageData;
import cn.tedu.bs.admin.service.IBankService;
import cn.tedu.bs.admin.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@RequestMapping("/bank")
@Api(tags = "13. 库存管理模块")
@Slf4j
@Validated
public class BankController {
    @Autowired
    private IBankService bankService;

    // http://localhost/bank/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加美容库存")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid BankAddNewParam  bankAddNewParam,MultipartFile file) {
        log.debug("开始处理【添加美容库存】的请求，参数：{}，和", bankAddNewParam);
        bankService.addNew(bankAddNewParam,file);
        return JsonResult.ok();
    }

    // http://localhost/bank/delete
    @PostMapping("/delete")
    @ApiOperation("根据ID删除美容库存")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "美容库存ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除美容库存失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【删除库存详情】的业务，参数：{}", id);
        bankService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost/bank/update
    @PostMapping("/update")
    @ApiOperation("修改美容库存详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "美容库存ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的美容库存ID值！") Long id,
                                     @Valid BankUpdateInfoParam BankUpdateInfoParam,MultipartFile file) {

        log.debug("开始处理【修改美容库存详情】的业务，参数：{}", BankUpdateInfoParam);
        bankService.updateInfoById(id, BankUpdateInfoParam,file);
        return JsonResult.ok();
    }

    // http://localhost/bank/standard
    @GetMapping("/standard")
    @ApiOperation("根据ID查询库存详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "库存ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询库存详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询库存详情】的请求，参数：{}", id);
        BankStandardVO result = bankService.getStandardById(id);
        return JsonResult.ok(result);
    }

    // http://localhost/bank/list
    @GetMapping("/list")
    @ApiOperation("查询库存列表")
    @ApiOperationSupport(order = 420)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
//            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "long")
//    })
    public JsonResult list(@Range(min = 1, message = "查询库存列表失败，请提供正确的页码值！") Integer page,
                           @Range(min = 5, message = "查询库存列表失败，请提供正确的数量！") Integer pageSize) {
        log.debug("开始处理【查询库存列表】的请求，页码：{},每页数量:{}", page, pageSize);
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 5) {
            page = 5;
        }
        PageData<BankListItemVO> pageData = bankService.list(page, pageSize);
        return JsonResult.ok(pageData);
    }

    // http://localhost/bank/list_by_name
    @GetMapping("/list_by_name")
    @ApiOperation("根据名称查询库存列表")
    @ApiOperationSupport(order = 430)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
//    })
    public JsonResult listByName(@Range(min = 1, message = "查询库存列表失败，请提供正确的页码值！") Integer page,
                                 @Range(min = 5, message = "查询库存列表失败，请提供正确的数量！") Integer pageSize,
                                 String name) {
        log.debug("开始处理【根据名称查询库存列表】的请求，页码：{},每页记录：{}, 名称：{}", page, pageSize, name);
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 5) {
            page = 5;
        }
        PageData<BankListItemVO> pageData;
        if (name == null) {
            pageData = bankService.list(page, pageSize);
        } else {
            pageData = bankService.getListByProductName(page, pageSize, name);
        }
        return JsonResult.ok(pageData);
    }

}
